/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTE:
 * Derivative work based on the Google IO 2011 Schedule app released for open source
 * http://code.google.com/p/iosched/
 */

package edu.bsu.android.gtv.apps.bconnected.util;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import edu.bsu.android.gtv.apps.bconnected.R;

/**
 * An assortment of UI helpers.
 * @author Kyle Parker, University Computing Services
 */
public class UIUtils {

    /**
     * Populate the title with the appropriate text.  This appears at the top of the screen for each activity.
     */
    public static void setTitle(Activity activity, String title) {
        ((TextView) activity.findViewById(R.id.title_bar_title)).setText(title);
    }
	
    /**
     * Generic display utility for the textviews
     * @param view
     * @param resourceID
     * @param value
     */
	public static void displayTextView(View view, int resourceID, String value) {    	
    	if (value != null) {
    		if (value.trim().length() == 0) {
    			((TextView) view.findViewById(resourceID)).setVisibility(View.GONE);
	    	} else {
		        ((TextView) view.findViewById(resourceID)).setVisibility(View.VISIBLE);
		        ((TextView) view.findViewById(resourceID)).setText(value);
	    	}
    	} else {
	        ((TextView) view.findViewById(resourceID)).setVisibility(View.GONE);
    	}
	} 
	
	/**
	 * Calls the Google Maps intent - based on a URL to function properly on GTV
	 * This will open the Chrome browser and pin-point the location based on the GPS coordinates and the building name
	 * @param buildingDesc
	 * @param latitude
	 * @param longitude
	 * @return a new intent
	 */
    public static Intent getGoogleMapsIntent(String buildingDesc, String latitude, String longitude) {
		if (latitude != null && longitude != null) {
			String uri = "http://maps.google.com/?q=" + longitude + "," + latitude + " (" + buildingDesc + ")";

			return new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
		} else {
			return null;
		}
    }
    
    /**
     * Formats the address based on the available string data
     * @param address1
     * @param address2
     * @param city
     * @param state
     * @param zip
     * @return address
     */
    public static String formatAddress(String address1, String address2, String city, String state, String zip) {
    	String address = "";
    	
    	if (address1.length() > 0) {
    		address = address1;
    		
    		if (address2.length() > 0) {
    			address = address + "\n" + address2;
    		}
    		
    		address = address + "\n" + city + ", " + state + " " + zip;
    	}  else {
    		address = "Ball State University\nMuncie, IN 47306";
    	}
    	
    	return address;
    }

    /**
     * Gets the last ID from the shared preferences - this is used to determine what tab to show when a user visits an info page.
     * This comes in helpful when the user selects (for example) the "classmates" tab, selects a classmate to view their info, and then
     * returns to the course info - the "classmates" tab remains selected.
     * @param activity
     * @param key
     * @return
     */
    public static int getLastID(Activity activity, String key) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
        return sp.getInt(key, 0);
    }

    /**
     * Sets the last ID in the shared preferences for the tab selected
     * @param activity
     * @param key
     * @param value
     */
    public static void setLastID(Activity activity, String key, int value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
        sp.edit().putInt(key, value).commit();
    }
    
    /**
     * Build a {@link android.view.View} to be used as a tab indicator, setting the requested string resource as
     * its label.
     * @param textRes
     * @param activity
     * @param rootView
     * @return View
     */
    public static View buildIndicator(int textRes, Activity activity) {
        final TextView indicator = (TextView) activity.getLayoutInflater()
                .inflate(R.layout.tab_indicator, (ViewGroup) activity.findViewById(android.R.id.tabs), false);
        indicator.setText(textRes);
        return indicator;
    }
}
